<template>
	<view>
        <uni-section title="datetime" type="line" subTitle="日期接口支持节假日、农历等，可用于农历生日提醒"></uni-section>
		<view>
			<uni-calendar class="uni-calendar--hook" :lunar="info.lunar" :selected="info.selected" :showMonth="info.showMonth" @change="change" @monthSwitch="monthSwitch" />
		</view>
        <text class="uni-text uni-padding-wrap">{{dayInfo}}</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
                dayInfo: '',
                calendarInfo: [],//缓存已请求的年月
				info: {
					lunar: true,
                    showMonth: true,
					selected: [],
				}
			}
		},
		onReady() {
            let today = new Date(), year = today.getFullYear(), month = today.getMonth()+1
			this.datetime(year+'-'+(month<10?'0'+month:month)+'-'+today.getDate())
			setTimeout(() => {
                this.calendar(year+'-'+(month<10?'0'+month:month))
			}, 1000)
		},
		methods: {
			change(e) {
                this.datetime(e.fulldate)
			},
			monthSwitch(e) {
                this.calendar(e.year+'-'+(e.month<10?'0'+e.month:e.month))
			},
            datetime(day){
                console.log('datetime: '+day)
                uni.request({
                	url: getApp().globalData.api.service+'/datetime/info.json',
                	dataType: 'application/json',
                	data: {
                		day: day
                	},
                	success: (res) => {
                        if(res.data){
                           let data = JSON.parse(res.data)
                           this.dayInfo = data.ganzhi + data.shengxiao + '年，' + data.chinese + (!data.holiday ? '' : '，'+data.holiday)
                        }
                	}
                });
            },
            calendar(yearMonth){
                if(this.calendarInfo.indexOf(yearMonth)>-1) return;
                console.log('calendar: '+yearMonth)
                this.calendarInfo.push(yearMonth)
                uni.request({
                    url: getApp().globalData.api.service+'/datetime/calendar.json',
                    dataType: 'application/json',
                    data: {
                        day: yearMonth+'-01'
                    },
                    success: (res) => {
                        if(res.data){
                           let data = JSON.parse(res.data)
                           for(let day in data){
                               let info = data[day]
                               let select = (info.isweekend && info.isworkday) || (!info.isweekend && !info.isworkday) || info.isholiday;
                               //console.log(day+' select: '+select+','+JSON.stringify(info))
                               if(select){
                                   this.info.selected.push({
                                       date: day,
                                       info: info.isholiday ? '假' : (info.isworkday ? '班' : '休')
                                   })
                               }
                           }
                        }
                    }
                })
            }
		}
	}
</script>

<style>

</style>